// pages/order_list/order_list.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id:'',
    evaluation:[],//评价结果
    k:'',
    u:'',
    image_list:[],
    is_pingjia:0,
    is_show_time: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var k = wx.getStorageSync('key')
    var u = wx.getStorageSync('u')
    var order_id = options.order_id
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=evaluation&act=add&typ=json',
      method: 'GET',
      data: { k: k, u: u, order_id: order_id},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var arr = res.data.data.order_goods
        
        var evaluation = []
        var image_list = []
        for (var i in arr){
          var info = []
          var z = []
          info.push(arr[i].id)
          info.push(5)
          info.push('good')
          info.push('')
          info.push('')
          info.push('')
          z.push('')
          z.push('')
          z.push('')
          z.push('')
          z.push('')
          evaluation.push(info)
          image_list.push(z)
        }
        
        that.setData({
          order_id: order_id,
          member_info:res.data.data,
          evaluation: evaluation,
          k:k,
          u:u,
          image_list: image_list,
          is_show_time: 0
        })
      }
    })
  },
  
  //提交评价
  submit:function(e){
    var that = this

    var evaluation = that.data.evaluation
 
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Goods_Evaluation& met=addGoodsEvaluation&typ=json',
      method: 'GET',
      data: {
         k: that.data.k,
         u: that.data.u,
         evaluation: evaluation,
         wxapp:1
        },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 200) {
          app.show('评价成功', 'success') 
          that.setData({
            is_pingjia:1
          })
          
        } else {
          app.show('评价失败', 'loading')
        }
      }
    })
  },
  //评星
  bind:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var nums = e.currentTarget.dataset.nums
    var evaluation = that.data.evaluation
    evaluation[id][1] = nums
   
    that.setData({
      evaluation: evaluation
    })
  },
  //点击选择要上传的图片
  image:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var image_list = that.data.image_list
    var evaluation = that.data.evaluation
    var z = e.currentTarget.dataset.z
    wx.chooseImage({ 
      count: 1, 
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有'original', 'compressed' 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        var tempFilePaths = res.tempFilePaths
        var re = ''
       
        wx.uploadFile({ //上传文件的接口;
          url: app.configData.shop.api_url + '/index.php?ctl=Upload&action=uploadImage&typ=json', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'upfile',
          header: {
            'content-type': 'multipart/form-data'
          },
          formData: {
            'user': 'test'
          },
          success: function (res) {
console.log(res)
            var data = res.data

             re = JSON.parse(res.data);
             image_list[id][z] = re.url
             if (evaluation[id][4]) {
               var img = evaluation[id][4] + "," + re.url
             } else {
               var img = re.url + " "
             }
             evaluation[id][4] = img

             that.setData({
               textHidden: true,
               evaluation: evaluation,
               image_list: image_list,
               photoHidden: false
             })
          }
        })
      
      }
    })
  },
  
  //评论
  contentInput: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var evaluation = that.data.evaluation
    evaluation[id][3] = e.detail.value
  },
})
